import React, { useState, useEffect, useRef } from 'react'
import { withRouter } from 'react-router-dom'
import { connect } from 'react-redux'
import action from '../../store/action'
import request from '../../utils/request';
import './index.less'
function Login(props) {
    const [count, setCount] = useState({
        user: '',
        pass: '',
        flag: false
    })
    useEffect(() => {
        props.getUser()
    }, [])
    let box = useRef()
    return (
        <div className="login">
            <div className="Login_top">
                <p onClick={() => {
                    props.history.go(-1)
                }}>x</p>
                <h2>密码登录</h2>
            </div>
            <div className="inp">
                <p><input type="text" placeholder="手机/用户名" value={count.user} onChange={(e) => {
                    setCount({ ...count, user: e.target.value })
                }} /></p>
                <p><input type="password" placeholder="密码" value={count.pass} onChange={(e) => {
                    setCount({ ...count, pass: e.target.value })
                }} /></p>
            </div>
            {
                count.flag ? <div className="Ts">
                    账号或密码错误
                </div> : null
            }
            <div className="btn">
                <button disabled={count.user && count.pass ? false : true}
                    onClick={() => {
                        console.log(props);
                        props.login.User.forEach(item => {
                            if (item.user === count.user && item.pass === count.pass) {
                                console.log(item);
                                request({
                                    url: '/froms/diyi',
                                    method: 'GET',
                                    params: { id: item.id }
                                }).then(res => {
                                    // localStorage.setItem('token', JSON.stringify(res))
                                })
                                setCount({ ...count, flag: false })
                                sessionStorage.setItem('token', JSON.stringify(item))
                                props.history.push('/home')
                            } else {
                                setCount({ ...count, flag: true })
                            }
                        })
                    }}>登录</button>
            </div>
            <div className="zc">
                还没有账号？<span onClick={() => {
                    props.history.push('/register')
                }}>注册</span>
            </div>
        </div >
    )

}
export default connect(state => state, action.login)(withRouter(Login))
